package com.api.customization.unionpay.market.web;

import com.customization.unionpay.market.entity.Response;
import com.customization.unionpay.market.service.ClearFundDetailService;
import com.customization.unionpay.market.service.impl.ClearFundDetailServiceImpl;
import com.engine.common.util.ParamUtil;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.util.Map;

/**
 * 清算资金明细台账的接口
 */

@Path("/market/clear/fund")
public class ClearFundDetailController {

    @GET
    @Path("/getDailyDetail")
    @Produces(MediaType.APPLICATION_JSON)
    public String getDailyDetailByDate(@Context HttpServletRequest request,
                                       @Context HttpServletResponse response){

        ClearFundDetailService service = new ClearFundDetailServiceImpl();
        User user = HrmUserVarify.checkUser(request,response);
        Map<String,Object> params = ParamUtil.request2Map(request);
        String date = params.get("date").toString().replace("-","");
        return JSON.toJSONString(Response.success(service.getDailyDetailByDate(date,user)));
    }
    @POST
    @Path("/upload")
    @Produces(MediaType.MULTIPART_FORM_DATA)
    public String


    upload(@Context HttpServletRequest request, @Context HttpServletResponse response){
        try {
            InputStream inputStream = request.getInputStream();
            String fileName = request.getParameter("filename");

            String uploadDir = "D:\\test";
            File file = new File(uploadDir + File.separator + fileName);
            OutputStream out = new FileOutputStream(file);

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();

            return "File " + fileName + " uploaded successfully.";
        } catch (IOException e) {
            e.printStackTrace();
            return "Error uploading file: " + e.getMessage();
        }
    }
}
